//
//  GSSHome.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "GSSHome.h"

#import "NSString+GSSAdditions.h"
#import "FormattingUtilities.h"
#import "GSSCommunicator.h"

@implementation GSSHome

- (id)initWithURI:(NSString *)aURI {
    return [super initWithURI:aURI withType:GSSObjectTypeHome];
}

//MARK: -
//MARK: Properties

- (NSString *)rootURI {
    return [[meta objectForKey:@"fileroot"] stringByAppendingSlash];
}

- (NSString *)trashURI {
    return [[meta objectForKey:@"trash"] stringByAppendingSlash];
}

- (NSString *)sharedURI {
    return [[meta objectForKey:@"shared"] stringByAppendingSlash];
}

- (NSString *)othersURI {
    return [[meta objectForKey:@"others"] stringByAppendingSlash];
}

- (NSString *)tagsURI {
    return [[meta objectForKey:@"tags"] stringByAppendingSlash];
}

- (NSString *)groupsURI {
    return [[meta objectForKey:@"groups"] stringByAppendingSlash];
}

- (NSString *)announcement {
    return [meta objectForKey:@"announcement"];
}


- (unsigned long long)totalFiles {
    return [[[meta objectForKey:@"quota"] objectForKey:@"totalFiles"] unsignedLongLongValue];
}

- (unsigned long long)totalBytes {
    return [[[meta objectForKey:@"quota"] objectForKey:@"totalBytes"] unsignedLongLongValue];
}

- (unsigned long long)bytesRemaining {
    return [[[meta objectForKey:@"quota"] objectForKey:@"bytesRemaining"] unsignedLongLongValue];
}


//MARK: -

- (void)updateMeta:(NSDictionary *)newMeta {
    if (!newMeta)
        return;
    
    [self willChangeValueForKey:@"meta"];
    [self willChangeValueForKey:@"children"];

    [meta addEntriesFromDictionary:newMeta];
    [children removeAllObjects];

    if (self.rootURI && self.sharedURI && self.othersURI && self.trashURI) {
        GSSCommunicator *communicator = [GSSCommunicator defaultCommunicator];
        
        GSSObject *fileroot = [communicator objectWithURI:self.rootURI withType:GSSObjectTypeRoot];
        if (!fileroot.name)
            fileroot.name = self.name;
        [children addObject:fileroot];
        
        GSSObject *shared = [communicator objectWithURI:self.sharedURI withType:GSSObjectTypeShared];
        if (!shared.name)
            shared.name = LocalizedString(@"My shared");
        [children addObject:shared];
        
        GSSObject *others = [communicator objectWithURI:self.othersURI withType:GSSObjectTypeOthers];
        if (!others.name)
            others.name = LocalizedString(@"Other's shared");
        [children addObject:others];
        
        GSSObject *trash = [communicator objectWithURI:self.trashURI withType:GSSObjectTypeTrash];
        if (!trash.name)
            trash.name = LocalizedString(@"Trash");
        [children addObject:trash];
    }
    
    [self didChangeValueForKey:@"meta"];
    [self didChangeValueForKey:@"children"];
}

@end
